javascript
小名的开源项目【EamonVenti】0.0篇 —— 学习如何搭建一个简单的SpringCloud架构,体验微服务的强大!
但是小名從來沒有接觸過,所以想著先體驗(yàn)一下微服務(wù)的強(qiáng)大!
這篇文章是小名的SpringCloud啟航篇0.0🎊
本文僅涉及到SimpleStructure分支
文章目錄
- 一 、 創(chuàng)建 父模塊:
- 1.創(chuàng)建一個maven項(xiàng)目
- 2. 父項(xiàng)目創(chuàng)建完成,以后的子項(xiàng)目都應(yīng)該繼承這個項(xiàng)目,在pom.xml文件添加依賴:
- 3. 啟動 父模塊
- 二 、 創(chuàng)建 注冊中心(Eureka服務(wù))
- 1. 在父模塊下,創(chuàng)建新模塊作為注冊中心
- 2. 啟動并驗(yàn)證 注冊中心 是否可用 http://localhost:8081
- 3. 瀏覽器中出現(xiàn)如下界面,恭喜你,成功了!
- 三 、 創(chuàng)建 生產(chǎn)者
- 1. 在父模塊下,創(chuàng)建新模塊作為生產(chǎn)者
- 2.啟動 producer,并輸入:
- 3. 刷新 注冊中心頁面,可以看到 服務(wù)已經(jīng)注冊到注冊中心
- 四 、 創(chuàng)建 消費(fèi)者:
- 1. 在父模塊下,創(chuàng)建新模塊作為消費(fèi)者
- 2. 右鍵啟動“ConsumerApplication”
- 3. 刷新 注冊中心頁面,可以看到 服務(wù)已經(jīng)注冊到注冊中心
一 、 創(chuàng)建 父模塊:
1.創(chuàng)建一個maven項(xiàng)目
(1)打開idea,File —> New project —>Maven
(2)填寫 項(xiàng)目名稱、坐標(biāo)(groupid和artifactId)
(3) 選擇Maven倉庫地址
2. 父項(xiàng)目創(chuàng)建完成,以后的子項(xiàng)目都應(yīng)該繼承這個項(xiàng)目,在pom.xml文件添加依賴:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.Eamon</groupId><artifactId>EamonVenti</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.3.RELEASE</version></parent><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>3. 啟動 父模塊
(1)將war放到Tomcat
(2) 部署本地 war 到Tomcat:
(3)啟動Tomcat
(4)驗(yàn)證:網(wǎng)址輸入 http://localhost:8080/ 出現(xiàn)“Hello,World!”,啟動成功!
二 、 創(chuàng)建 注冊中心(Eureka服務(wù))
1. 在父模塊下,創(chuàng)建新模塊作為注冊中心
(1) 如下圖,在EamonVenti上右鍵創(chuàng)建新模塊
(2)選擇 Spring Initializr
(3) 按下圖操作:
(4)pom中修改或添加的注冊中心的依賴
注意:子模塊依賴父模塊時,如下圖一定要一 一對應(yīng):
除了上圖以外,還要手動添加如下依賴:
<!--添加注冊中心的依賴--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId><version>1.3.0.RELEASE</version></dependency>(5)創(chuàng)建配置文件application.yml
spring:application:name: eureka-server#開啟權(quán)限認(rèn)證security:basic:enabled: trueuser:name: rootpassword: rootserver:host: localhostport: 8081 eureka:client:#此項(xiàng)目不作為客戶端注冊register-with-eureka: falsefetch-registry: falseservice-url:#開啟權(quán)限驗(yàn)證后Eureka地址為 用戶名:密碼@地址:端口號,如未開啟權(quán)限驗(yàn)證則直接使用 地址:端口號defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${server.host}:${server.port}/eureka其中:
eureka:client:register-with-eureka: falsefetch-registry: false意義是,禁止 eureka server 自己注冊自己
(6)啟動項(xiàng)中添加注解 @EnableEurekaServer
@EnableEurekaServer//添加該注解,申明此處為服務(wù)注冊中心 @SpringBootApplication public class RegisterApplication {public static void main(String[] args) {SpringApplication.run(RegisterApplication.class, args);}}其中:
@SpringBootApplication注解是 : SpringBoot的一個組合注解,包含@Configuration、@EnableAutoConfiguration、@ComponentScan
PS:如果開啟了權(quán)限驗(yàn)證并且SpringBoot版本為2.0以上的話還需要一個操作,因?yàn)?.0默認(rèn)開啟了csrf,如果我們現(xiàn)在直接啟動Eureka服務(wù)的話客戶端是注冊不上的,所以需要把csrf關(guān)閉
在cn.org.zhixiang包下新建security包,新建WebSecurityConfigurer類
@EnableWebSecurity public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.csrf().disable();super.configure(http);} }2. 啟動并驗(yàn)證 注冊中心 是否可用 http://localhost:8081
3. 瀏覽器中出現(xiàn)如下界面,恭喜你,成功了!
三 、 創(chuàng)建 生產(chǎn)者
1. 在父模塊下,創(chuàng)建新模塊作為生產(chǎn)者
(1) 步驟跟創(chuàng)建注冊中心一樣,區(qū)別在命名:producer
(2) pom中添加依賴
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId><version>1.4.4.RELEASE</version> </dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId> </dependency>(3) application.yml中配置相關(guān)信息
spring:application:name: produce server:port: 8082eurekaServer:host: localhostport: 8081user: rootpassword: rooteureka:client:#將此項(xiàng)目注冊到Eureka服務(wù)register-with-eureka: trueservice-url: http://${eurekaServer.user}:${eurekaServer.password}@${eurekaServer.host}:${eurekaServer.port}/eureka(4) 啟動項(xiàng)中 添加注解 @EnableEurekaClient,聲明自己為生產(chǎn)者
@SpringBootApplication @EnableEurekaClient//聲明自己為生產(chǎn)者 public class ProduceApplication {public static void main(String[] args) {SpringApplication.run(ProduceApplication.class, args);}}(5) 創(chuàng)建User實(shí)體類以及UserController,目錄如下圖:
User實(shí)體類:
public class User {private long id;private String name;private int age;public long getId() {return id;}public void setId(long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;} }UserController類:
@RestController @RequestMapping("/user") public class UserController {@GetMapping(value = "/getUser/{id}")public User getUser(@PathVariable Long id){User user=new User();user.setId(id);user.setName("小名");user.setAge(2);return user;}@GetMapping(value = "/getName")public String getName(){return "小名";} }注解解釋:
- @RestController注解:@ResponseBody + @Controller
- @GetMapping:相當(dāng)于@RequestMapping(value="", method = RequestMethod.GET)
- @PathVariable:可以將URL中占位符參數(shù){xxx}綁定到處理器類的方法形參中
2.啟動 producer,并輸入:
瀏覽器輸入:http://localhost:8082/user/getUser/3
或
瀏覽器輸入:http://localhost:8082/user/getName
返回:小名
3. 刷新 注冊中心頁面,可以看到 服務(wù)已經(jīng)注冊到注冊中心
四 、 創(chuàng)建 消費(fèi)者:
1. 在父模塊下,創(chuàng)建新模塊作為消費(fèi)者
?
(1)前面的步驟同生產(chǎn)者
(2)在pom.xml中添加依賴
(3)application.yml 中添加配置信息
spring:application:name: consumer server:port: 8083eurekaServer:host: localhostport: 8081user: rootpassword: rooteureka:client:#將此項(xiàng)目注冊到Eureka服務(wù)register-with-eureka: trueservice-url:defaultZone: http://${eurekaServer.user}:${eurekaServer.password}@${eurekaServer.host}:${eurekaServer.port}/eureka(4)修改UserController:
@RestController @RequestMapping("/user") public class UserController {@Autowiredprivate RestTemplate restTemplate;@GetMapping(value = "/getUser/{id}")public User getUser(@PathVariable Long id){return restTemplate.getForObject("http://produce/user/getUser/"+id,User.class);}@GetMapping(value = "/getName")public String getName(){return "小名";} }(5)啟動項(xiàng)中 添加注解 @EnableDiscoveryClient,聲明自己為消費(fèi)者
@SpringBootApplication @EnableDiscoveryClient//聲明自己為消費(fèi)者 public class ConsumerApplication {@Bean@LoadBalanced//開啟負(fù)載均衡public RestTemplate restTemplate(){return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(ConsumerApplication.class, args);}}注解解釋:
-
RestTemplate類:是一個對于HTTP請求封裝的一個類,這個就是一個封裝原生API訪問一個URL的簡化版本。
-
@Bean注解:等同于以前在xml中配置的如下代碼
<beans> <bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/> </beans> -
@LoadBalanced注解:開啟Eureka的負(fù)載均衡
2. 右鍵啟動“ConsumerApplication”
瀏覽器輸入:http://localhost:8083/user/getUser/3
輸出:{“id”:3,“name”:“小名”,“age”:2}
3. 刷新 注冊中心頁面,可以看到 服務(wù)已經(jīng)注冊到注冊中心
Ending😜~
本文僅涉及到SimpleStructure分支
后期小名隨著陸續(xù)地學(xué)習(xí),會重新搭建項(xiàng)目框架,本文只是“品嘗一下SpringCloud的味道~ ”現(xiàn)在可以肯定的是會用到公司選用的Nacos,其他的小名還在學(xué)習(xí)中。所以,敬請期待~😊?
此文僅涉及到倉庫中的SimpleStructure分支,希望大家可以持續(xù)關(guān)注小名,支持一下小名,給我的文章點(diǎn)贊👍、評論?、收藏🤞;給我的代碼倉庫點(diǎn)一個小Star?,謝謝大家啦~???
總結(jié)
以上是生活随笔為你收集整理的小名的开源项目【EamonVenti】0.0篇 —— 学习如何搭建一个简单的SpringCloud架构,体验微服务的强大!的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SylixOS 共用中断号机制
- 下一篇: 2020清华大学计算机复试线,2020清