日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

小名的开源项目【EamonVenti】0.0篇 —— 学习如何搭建一个简单的SpringCloud架构,体验微服务的强大!

發布時間:2023/12/20 javascript 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 小名的开源项目【EamonVenti】0.0篇 —— 学习如何搭建一个简单的SpringCloud架构,体验微服务的强大! 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
小名聽說,公司的下一個項目會用到SpringCloud。小名非常開心🎉
但是小名從來沒有接觸過,所以想著先體驗一下微服務的強大!
這篇文章是小名的SpringCloud啟航篇0.0🎊

本文僅涉及到SimpleStructure分支


文章目錄

        • 一 、 創建 父模塊:
          • 1.創建一個maven項目
          • 2. 父項目創建完成,以后的子項目都應該繼承這個項目,在pom.xml文件添加依賴:
          • 3. 啟動 父模塊
        • 二 、 創建 注冊中心(Eureka服務)
          • 1. 在父模塊下,創建新模塊作為注冊中心
          • 2. 啟動并驗證 注冊中心 是否可用 http://localhost:8081
          • 3. 瀏覽器中出現如下界面,恭喜你,成功了!
        • 三 、 創建 生產者
          • 1. 在父模塊下,創建新模塊作為生產者
          • 2.啟動 producer,并輸入:
          • 3. 刷新 注冊中心頁面,可以看到 服務已經注冊到注冊中心
        • 四 、 創建 消費者:
          • 1. 在父模塊下,創建新模塊作為消費者
          • 2. 右鍵啟動“ConsumerApplication”
          • 3. 刷新 注冊中心頁面,可以看到 服務已經注冊到注冊中心

一 、 創建 父模塊:

1.創建一個maven項目

(1)打開idea,File —> New project —>Maven

(2)填寫 項目名稱、坐標(groupid和artifactId)

(3) 選擇Maven倉庫地址

2. 父項目創建完成,以后的子項目都應該繼承這個項目,在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)驗證:網址輸入 http://localhost:8080/ 出現“Hello,World!”,啟動成功!

二 、 創建 注冊中心(Eureka服務)

1. 在父模塊下,創建新模塊作為注冊中心

(1) 如下圖,在EamonVenti上右鍵創建新模塊

(2)選擇 Spring Initializr

(3) 按下圖操作:

(4)pom中修改或添加的注冊中心的依賴

注意:子模塊依賴父模塊時,如下圖一定要一 一對應:

除了上圖以外,還要手動添加如下依賴:

<!--添加注冊中心的依賴--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId><version>1.3.0.RELEASE</version></dependency>

(5)創建配置文件application.yml

spring:application:name: eureka-server#開啟權限認證security:basic:enabled: trueuser:name: rootpassword: rootserver:host: localhostport: 8081 eureka:client:#此項目不作為客戶端注冊register-with-eureka: falsefetch-registry: falseservice-url:#開啟權限驗證后Eureka地址為 用戶名:密碼@地址:端口號,如未開啟權限驗證則直接使用 地址:端口號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)啟動項中添加注解 @EnableEurekaServer

@EnableEurekaServer//添加該注解,申明此處為服務注冊中心 @SpringBootApplication public class RegisterApplication {public static void main(String[] args) {SpringApplication.run(RegisterApplication.class, args);}}

其中:
@SpringBootApplication注解是 : SpringBoot的一個組合注解,包含@Configuration、@EnableAutoConfiguration、@ComponentScan

PS:如果開啟了權限驗證并且SpringBoot版本為2.0以上的話還需要一個操作,因為2.0默認開啟了csrf,如果我們現在直接啟動Eureka服務的話客戶端是注冊不上的,所以需要把csrf關閉

在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. 啟動并驗證 注冊中心 是否可用 http://localhost:8081

3. 瀏覽器中出現如下界面,恭喜你,成功了!

三 、 創建 生產者

1. 在父模塊下,創建新模塊作為生產者

(1) 步驟跟創建注冊中心一樣,區別在命名: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中配置相關信息

spring:application:name: produce server:port: 8082eurekaServer:host: localhostport: 8081user: rootpassword: rooteureka:client:#將此項目注冊到Eureka服務register-with-eureka: trueservice-url: http://${eurekaServer.user}:${eurekaServer.password}@${eurekaServer.host}:${eurekaServer.port}/eureka

(4) 啟動項中 添加注解 @EnableEurekaClient,聲明自己為生產者

@SpringBootApplication @EnableEurekaClient//聲明自己為生產者 public class ProduceApplication {public static void main(String[] args) {SpringApplication.run(ProduceApplication.class, args);}}

(5) 創建User實體類以及UserController,目錄如下圖:

User實體類:

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:相當于@RequestMapping(value="", method = RequestMethod.GET)
  • @PathVariable:可以將URL中占位符參數{xxx}綁定到處理器類的方法形參中
2.啟動 producer,并輸入:

瀏覽器輸入:http://localhost:8082/user/getUser/3

瀏覽器輸入:http://localhost:8082/user/getName
返回:小名

3. 刷新 注冊中心頁面,可以看到 服務已經注冊到注冊中心

四 、 創建 消費者:

1. 在父模塊下,創建新模塊作為消費者

?
(1)前面的步驟同生產者
(2)在pom.xml中添加依賴

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId><version>1.4.4.RELEASE</version></dependency><!--提供客戶端的軟件負載均衡--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-ribbon</artifactId><version>1.4.2.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>

(3)application.yml 中添加配置信息

spring:application:name: consumer server:port: 8083eurekaServer:host: localhostport: 8081user: rootpassword: rooteureka:client:#將此項目注冊到Eureka服務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)啟動項中 添加注解 @EnableDiscoveryClient,聲明自己為消費者

@SpringBootApplication @EnableDiscoveryClient//聲明自己為消費者 public class ConsumerApplication {@Bean@LoadBalanced//開啟負載均衡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的負載均衡

2. 右鍵啟動“ConsumerApplication”

瀏覽器輸入:http://localhost:8083/user/getUser/3
輸出:{“id”:3,“name”:“小名”,“age”:2}

3. 刷新 注冊中心頁面,可以看到 服務已經注冊到注冊中心


Ending😜~

本文僅涉及到SimpleStructure分支

后期小名隨著陸續地學習,會重新搭建項目框架,本文只是“品嘗一下SpringCloud的味道~ ”現在可以肯定的是會用到公司選用的Nacos,其他的小名還在學習中。所以,敬請期待~😊
?
此文僅涉及到倉庫中的SimpleStructure分支,希望大家可以持續關注小名,支持一下小名,給我的文章點贊👍、評論?、收藏🤞;給我的代碼倉庫點一個小Star?,謝謝大家啦~???

總結

以上是生活随笔為你收集整理的小名的开源项目【EamonVenti】0.0篇 —— 学习如何搭建一个简单的SpringCloud架构,体验微服务的强大!的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。