你好! 這是你第一次使用 Markdown編輯器 所展示的歡迎頁。如果你想學習如何使用Markdown編輯器, 可以仔細閱讀這篇文章,瞭解一下Markdown的基本語法知識。
按照截圖來,先建立一個根項目
點擊自己的項目右鍵 創建module
記得點擊enable auto-import自動導包
在enueka gradle 寫入下面這個配置導入相關包 (注意
group ‘com.rainteas’
version ‘1.0-SNAPSHOT’) 不要改
這是build.gradle
plugins { id 'org.springframework.boot' version '2.1.3.RELEASE' id 'java' } apply plugin: 'io.spring.dependency-management' group 'com.rainteas' version '1.0-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenCentral() } ext { set('springCloudVersion', 'Greenwich.SR1') } dependencies { implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server' testImplementation 'org.springframework.boot:spring-boot-starter-test' testCompile group: 'junit', name: 'junit', version: '4.12' } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } }
build.gradle 配置文件
plugins { id 'org.springframework.boot' version '2.1.3.RELEASE' id 'java' } apply plugin: 'io.spring.dependency-management' group 'com.rainteas' version '1.0-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenCentral() } ext { set('springCloudVersion', 'Greenwich.SR1') } dependencies { implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server' implementation 'org.springframework.cloud:spring-cloud-starter-netflix-zuul' testImplementation 'org.springframework.boot:spring-boot-starter-test' testCompile group: 'junit', name: 'junit', version: '4.12' } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } }
build.gradle
plugins { id 'org.springframework.boot' version '2.1.3.RELEASE' id 'java' } group 'com.rainteas' version '1.0-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenCentral() } ext { set('springCloudVersion', 'Greenwich.SR1') } dependencies { implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server' implementation 'org.springframework.cloud:spring-cloud-starter-openfeign:2.0.2.RELEASE' compileOnly 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' testCompile group: 'junit', name: 'junit', version: '4.12' } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } }
自己創建一個application.yml 將這個寫進去
server: port: 8762 eureka: client: #表明自己屬於服務中心主體 registerWithEureka: false #是否將自身註冊 fetchRegistry: false #表示自己就是服務註冊中心,我的職責就是爲父服務實例,並不需要去檢索服務 server: enable-self-preservation: false
同上 建立一個application.yml
spring: application: name: test-gateway eureka: client: service-url: defaultZone: http://localhost:8762/eureka instance: instance-id: test-gateway #自定義服務名稱信息 prefer-ip-address: true #訪問路徑可以顯示ip地址 #註冊服務使用ip地址 server: port: 8080 zuul: routes: testProject: path: /testShop/** serviceId: test-shop testUser: path: /testUser/** serviceId: test-user
同上
spring: application: name: test-user server: port: 8081 eureka: client: service-url: defaultZone: http://localhost:8762/eureka
這樣就完成了 下面是例子 testShop是網關,後面是方法地址
http://localhost:8080/testShop/shophello
先寫一個接口
把projectService 注入進去
然後直接調用即可