Git Product home page Git Product logo

spring-cloud-in-practice's Introduction

Spring Cloud in Practice

This project can be used as a starter for spring cloud microservices development. It is the microservices version of Spring Boot in Practice. It uses Spring Cloud Consul for service discovery and config management, Spring Cloud Gateway to implement api gateway, and ORY/Hydra for running an optional OAuth2 server. There is an article Spring Cloud 微服务开发指南 for learning this project.

Dependent frameworks and packages

  1. Spring Boot Web framework and server
  2. Spring Data JPA Access database
  3. Querydsl JPA Type safe dynamic sql builder
  4. Spring Security Authenticate and authrorize
  5. Spring Cloud Gateway Api gateway
  6. Spring Cloud Consul Service discovery
  7. Spring Cloud Circuit Breaker Circuit breaker
  8. ORY/Hydra OAuth2 server

Architecture

spring-cloud-micro-service-architecture

Modules

Name Description
Auth Authentication and authorization
User User management, follow relationship
Post Post management
File File management
Stat Stat of user and post

APIs

Path Method Description
/auth/login POST Login
/auth/logout GET Logout
/auth/logged GET Get logged user
/user/register POST Register user
/user/modify POST Modify logged user
/user/info GET Get user info
/user/sendMobileVerifyCode POST Send mobile verify code
/user/sendEmailVerifyCode POST Send email verify code
/user/follow POST Follow user
/user/unfollow POST Unfollow user
/user/following GET Following users of someone
/user/follower GET Fans of some user
/post/publish POST Publish post
/post/delete POST Delete post
/post/info GET Get post info
/post/published GET Get published posts of some user
/post/like POST Like post
/post/unlike POST Unlike post
/post/liked GET Liked posts of some user
/post/following GET Posts published by following users of someone
/file/upload POST Upload file
/file/info GET Get file meta info
/stat/ofUser GET Get user stat info
/stat/ofPost GET Get post stat info

The rest api service only return top objects, not return nested objects compared to the original monolith one.

How to run

This project need java 11+.

By local environment

Run a mysql service

If you use macOS, you can use brew install mysql to install mysql, and use brew services start mysql to start service at port 3306. Then you should create databases and tables for each microservices using sql files under db/migration/mysql.

  1. Use V1__Initial_create_dbs.sql to create databases and accounts to access these databases;
  2. Use V2__Initial_create_tables.sql to create tables;
  3. [Optional] Use V3__Initial_insert_data.sql to insert some data for testing;

Run a redis service

If you use macOS, you can use brew install redis to install redis, and use brew services start redis to start service at port 6379.

Run a consul service

If you use macOS, you can use brew install consul to install consul, and use brew services start consul to start service at port 8500.

Package all modules

./mvnw package

Start gateway and all backend services

java -jar gateway/target/spring-cloud-in-practice-gateway-1.0.0-SNAPSHOT.jar
java -jar user/target/spring-cloud-in-practice-user-1.0.0-SNAPSHOT.jar
java -jar post/target/spring-cloud-in-practice-post-1.0.0-SNAPSHOT.jar
java -jar file/target/spring-cloud-in-practice-file-1.0.0-SNAPSHOT.jar
java -jar stat/target/spring-cloud-in-practice-stat-1.0.0-SNAPSHOT.jar

Then you can access all APIs at http://localhost:8080. For example, you can register a new user using curl in terminal as following:

curl --request POST 'http://localhost:8080/user/register' \
--header 'Content-Type: application/json' \
--data-raw '{
	"username": "jaggerwang",
	"password": "123456"
}'

By docker compose

Package all modules

./mvnw package

Start all services at once

docker-compose up

If you repackaged any module, you should add --build option to enable the new jar package.

Then you can access all APIs at http://localhost:8080.

Integrate with OAuth2 Service

This application also support OAuth2 login, you need switch to oauth2 branch to enable this feature. We use ORY/Hydra to run an OAuth2 server, and any OAuth2 client can use this service to authenticate and authorize users.

Architecture

spring-cloud-micro-service-with-oauth2-architecture

Install hydra command

You need install hydra command first. You can use the following commands to install Hydra on macOS:

brew tap ory/hydra
brew install ory/hydra/hydra

Prepare database and start Hydra server

Connect to your local mysql server and create a database for Hydra.

CREATE DATABASE `scip_hydra`;

Then use the following commands to init database and run an OAuth2 server.

DSN=mysql://root:@tcp(localhost:3306)/scip_hydra hydra migrate sql -e --yes

STRATEGIES_ACCESS_TOKEN=jwt LOG_LEVEL=info SECRETS_SYSTEM=a2N4m0XL659TIrB2V3fJBxUED5Zv5zUQ DSN=mysql://scip_hydra:123456@tcp(localhost:3306)/scip_hydra URLS_SELF_ISSUER=http://localhost:4444/ URLS_LOGIN=http://localhost:8080/auth/hydra/login URLS_CONSENT=http://localhost:8080/auth/hydra/consent URLS_LOGOUT=http://localhost:8080/auth/hydra/logout TTL_ACCESS_TOKEN=12h TTL_REFRESH_TOKEN=720h hydra serve all --dangerous-force-http --dangerous-allow-insecure-redirect-urls 'http://localhost:8080/auth/hydra/login,http://localhost:8080/auth/hydra/consent,http://localhost:8080/auth/hydra/logout'

This step is not needed when run by docker compose.

Create OAuth2 clients

We will create two clients, one is for Hydra's builtin client, and the other is for the gateway service which can be used as an OAuth2 client.

hydra --endpoint 'http://localhost:4445/' clients create --id test --name 'Test' --secret E0g8oR7m711bGcvy --grant-types authorization_code,refresh_token,client_credentials,implicit --response-types token,code,id_token --scope openid,offline,profile --callbacks 'http://localhost:4446/callback'

hydra --endpoint 'http://localhost:4445/' clients create --id scip --name 'Spring Cloud in Practice' --secret ilxzM0AdA7BVaL7c --grant-types authorization_code,refresh_token,client_credentials,implicit --response-types token,code,id_token --scope offline,user,post,file,stat --callbacks 'http://localhost:8080/login/oauth2/code/hydra'

Test OAuth2 login with Hydra's builtin client

hydra token user --auth-url 'http://localhost:4444/oauth2/auth' --token-url 'http://localhost:4444/oauth2/token' --client-id test --client-secret E0g8oR7m711bGcvy --scope openid,offline,profile --redirect 'http://localhost:4446/callback'

It will auto open http://localhost:4446 to commence OAuth2 authorize flow.

Test OAuth2 login with any OAuth2 client

The gateway can also be used as an OAuth2 client, open login page at http://localhost:8080/login to commence an OAuth2 authorization flow.

Right now there is a bug which make OAuth2 login not working, you need remove config exceptionHandling in net.jaggerwang.scip.gateway.adapter.api.config.SecurityConfig.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.