Java位元組碼
阿新 • • 發佈:2020-08-17
原始碼地址: https://gitee.com/yangbuyi/bky_yby
接著上一次的nacos初步講解和安裝
任意門:https://www.cnblogs.com/Yangbuyi/p/13479767.html
如果啟動失敗的話 上一篇也是講解過的.
本文章開始服務註冊和發現
工程準備
建立一個父模組將裡面的src刪除即可留下.idea和pox.xml檔案
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.3.RELEASE</version> </parent> <!-- 全域性版本控制 --> <properties> <springCloud.version>Hoxton.SR7</springCloud.version> <springCloud-alibaba.version>2.1.1.RELEASE</springCloud-alibaba.version> </properties> <dependencyManagement> <dependencies> <!-- springCloud 版本控制 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${springCloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- 阿里 版本庫 依賴 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${springCloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
建立consumer工程模組和provide工程模組(呼叫者、服務者)
1.在consumer模組當中新增依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!-- 服務發現 nacos --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
2. 手寫啟動類
/** * @description: 楊不易網站:www.yangbuyi.top * @program: springcloudalibabaparent * @ClassName: AppConsumer * @create: 2020-08-16 20:57 * @author: yangbuyi * @since: JDK1.8 * @AppConsumer: **/@SpringBootApplication@Slf4j// nacos 服務發現 客戶端@EnableDiscoveryClientpublic class AppConsumer { public static void main(String[] args) throws UnknownHostException {ConfigurableApplicationContext application = SpringApplication.run(AppConsumer.class, args);Environment env = application.getEnvironment();String ip = InetAddress.getLocalHost().getHostAddress();String port = env.getProperty('server.port');String path = env.getProperty('server.servlet.context-path');String hj = env.getProperty('spring.profiles.active');log.info('\n----------------------------------------------------------
' +'Application yangbuyiCloud is running! Access URLs:
' +'Local: http://localhost:' + port + path + '/
' +'當前配置環境: 當前環境為:' + hj + '/
' +'----------------------------------------------------------'); } /** * @Description: 楊不易個人網址:http://yangbuyi.top * 功能描述: @LoadBalanced 讓rest 擁有負載均衡的能力 * @Param: @RestTemplate http請求服務 * @return: * @Author: TeouBle * @Date: 2020/8/16 20:58 */ @Bean @LoadBalanced public RestTemplate restTemplate() {return new RestTemplate(); }}
3.在啟動類上添加註解
nacos 服務發現 客戶端 @EnableDiscoveryClient
4.在配置檔案新增新增配置
server: port: 5000 servlet: context-path: /cloud-userspring: application: name: consumer-user cloud: nacos: discovery: #nacos服務的地址 不要加http # 可以寫一個地址 叢集會自動獲取服務 但是為了安全起見還是全部寫上 # server-addr: 127.0.0.1:8849,127.0.0.1:8850,127.0.0.1:8851 # 使用nginx反向代理 server-addr: 192.168.43.204:80 register-enabled: true # server-addr: localhost:8850name: 預設環境
上面看不懂的話也可直接寫這樣子的
server: port: 5000 servlet: context-path: /cloud-userspring: application: name: user-client# 將當前工程註冊到nacos上面 cloud: nacos: discovery: server-addr: localhost:8848 #nacos服務的地址 不要加http
5.啟動工程後, 在nacos當中查詢服務列表
nacos服務沒有下載的去觀看上一章節的nacos服務安裝和錯誤解決
https://www.cnblogs.com/Yangbuyi/p/13479767.html
6.使用相同方式 把provide服務註冊到nacos上
建立啟動類
package top.yangbuyi;/** * @description: 楊不易網站:www.yangbuyi.top * @program: springcloudalibabaparent * @ClassName: top.yangbuyi.AppConsumer * @create: 2020-08-16 20:57 * @author: yangbuyi * @since: JDK1.8 * @AppConsumer: **/@SpringBootApplication@Slf4j// nacos 服務發現 客戶端@EnableDiscoveryClientpublic class AppProvide { public static void main(String[] args) throws UnknownHostException {ConfigurableApplicationContext application = SpringApplication.run(AppProvide.class, args);Environment env = application.getEnvironment();String ip = InetAddress.getLocalHost().getHostAddress();String port = env.getProperty('server.port');String path = env.getProperty('server.servlet.context-path');String hj = env.getProperty('spring.profiles.active');log.info('\n----------------------------------------------------------
' +'Application yangbuyiCloud is running! Access URLs:
' +'Local: http://localhost:' + port + path + '/
' +'當前配置環境: 當前環境為:' + hj + '/
' +'----------------------------------------------------------'); } /** * @Description: 楊不易個人網址:http://yangbuyi.top * 功能描述: @LoadBalanced 讓rest 擁有負載均衡的能力 * @Param: @RestTemplate http請求服務 * @return: * @Author: TeouBle * @Date: 2020/8/16 20:58 */ @Bean @LoadBalanced public RestTemplate restTemplate() {return new RestTemplate(); }}
建立yml配置
server: port: 7000 servlet: context-path: /cloud-ybyspring: application: name: provide-goods # 服務名稱 服務ID cloud: nacos: discovery: server-addr: localhost:8085 # nacos服務地址 單個服務name: 預設環境